package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import api.getToken;
import api.textCheck;
import bean.Reject;
import bean.Review;
import bean.textResult;

public class checkTextServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		getToken mTextToken = new getToken();
		textCheck mTextCheck = new textCheck();
		String textString = req.getParameter("text");

		String messageString = null;
		resp.setContentType("text/html");
		resp.setCharacterEncoding("utf-8");
		if (textString.isEmpty()) {
			PrintWriter out = resp.getWriter();
			out.print("检验内容为空");
			return;
		}
		if (textString != null) {
			try {
				messageString = mTextCheck.postUrl(mTextToken.getAuth("text"), textString);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		PrintWriter out = resp.getWriter();
		Gson gson = new Gson();
		textResult mtextResult = gson.fromJson(messageString, textResult.class);
		String message = "";

		if (mtextResult == null) {
			message = message + "获取失败";
			out.print(message);
			return;
		}
		int spam = 0;
		try {
			spam = mtextResult.getResult().getSpam();
			if (spam == 0) {
				message = message + "不存在违禁词语\n";
			} else if (spam == 1) {
				message = message + "存在违禁词语\n";
			} else if (spam == 2) {
				message = message + "建议人工审核\n";
			}
		} catch (Exception e) {
		}
		// message = message + "log_id:" + mtextResult.getLog_id() + "\n";
		if (spam == 1) {
			for (int i = 0; i < mtextResult.getResult().getReject().size(); i++) {
				Reject reject = mtextResult.getResult().getReject().get(i);
				message = message + " 违禁词语:" + reject.getHit() + "分类:" + getScore(reject.getLabel()) + "\n";
			}
			for (int i = 0; i < mtextResult.getResult().getReview().size(); i++) {
				Review review = mtextResult.getResult().getReview().get(i);
				message = message + " 不确定词语:" + review.getHit() + "分类:" + getScore(review.getLabel()) + "\n";
			}

		} else if (spam == 2) {
			for (int i = 0; i < mtextResult.getResult().getReview().size(); i++) {
				Review review = mtextResult.getResult().getReview().get(i);
				message = message + "分类:" + getScore(review.getLabel()) + " 不确定词语:" + review.getHit() + "\n";
			}
		}
		System.out.println(message);
		out.print(message);
		System.out.println(messageString);

	}

	private String getScore(int i) {
		String result = null;
		switch (i) {
		case 1:
			result = "暴恐违禁";
			break;
		case 2:
			result = "文本色情	";
			break;
		case 3:
			result = "政治敏感	";
			break;
		case 4:
			result = "恶意推广	";
			break;
		case 5:
			result = "低俗辱骂	";
			break;
		default:
			break;
		}
		return result;
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		doGet(req, resp);
	}

}
